package com.zzs.blog.website.service.impl;

import com.github.pagehelper.PageHelper;
import com.zzs.blog.website.constant.WebConst;
import com.zzs.blog.website.dao.LogVoMapper;
import com.zzs.blog.website.model.Vo.LogVo;
import com.zzs.blog.website.model.Vo.LogVoExample;
import com.zzs.blog.website.service.LogService;
import com.zzs.blog.website.util.DateKit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author ZechariahZheng
 * @Date 2019/10/27 16:03
 * @Version 1.0
 * @description
 */
@Service
public class LogServiceImpl implements LogService {
    private static final Logger LOGGER = LoggerFactory.getLogger(LogServiceImpl.class);

    @Resource
    private LogVoMapper logDao;

    @Override
    public void insertLog(LogVo logVo) {
        logDao.insert(logVo);
    }

    @Override
    public void insertLog(String action, String data, String ip, Integer authorId) {
        LogVo logs = new LogVo();
        logs.setAction(action);
        logs.setData(data);
        logs.setIp(ip);
        logs.setAuthorId(authorId);
        logs.setCreated(DateKit.getCurrentUnixTime());
        logDao.insert(logs);
    }

    @Override
    public List<LogVo> getLogs(int page, int limit) {
        LOGGER.debug("Enter getLogs method:page={},limit={}", page, limit);
        if (page <= 0) {
            page = 1;
        }
        if (limit<1 || limit> WebConst.MAX_POSTS) {
            limit = 10;
        }
        LogVoExample logVoExample = new LogVoExample();
        logVoExample.setOrderByClause("id desc");
        PageHelper.startPage((page-1)*limit, limit);
        List<LogVo> logVos = logDao.selectByExample(logVoExample);
        LOGGER.debug("Exit getLogs method");
        return logVos;
    }
}
